var Kuo_apijson = {
    NAME: "apijson",
    TITLE: "低代码通信协议",
    DelBs: "off", // >0 //读取标识
    CacheTIme: 1, //缓存时间 调试使用
    Security: false,
    StatusCode: true,
    $DATA: {
        code: 401,
        msg: "",
        token: ""
    },
    Permissiondata: {},
    Rrequestdata: {},
    ArgumentsValidation: async (Arguments, $method, $Grade) => {
        //循环监测数据
        if (!Arguments || count(Arguments) < 1) {
            return true;
        }

        for (let key in Arguments) {

            let data = Arguments[key];
            if (typeof (data) != 'object') {
                continue;
            }
            if (key.indexOf('[]') > -1) {

                if (key == '[]') {

                    if (typeof (data) == 'object') {

                        let Validation = await Kuo_apijson.ArgumentsValidation(data, $method, $Grade);
                        if (Validation) {
                            return true;
                        }
                    }

                } else {

                    let Validation = await Kuo_apijson.PermissionValidation($method, key.replace('[]', ''), $Grade);
                    if (Validation) {
                        return true;
                    }
                    if (typeof (data) == 'object') {

                        Validation = await Kuo_apijson.ArgumentsValidation(data, $method, $Grade);
                        if (Validation) {
                            return true;
                        }
                    }
                }

            } else {


                let Validation = await Kuo_apijson.PermissionValidation($method, key, $Grade);
                if (Validation) {
                    return true;
                }
            }
        }

        return false;

    }, PermissionValidation: async ($method, $TAGS, $Grade) => {
        //验证get 数据
        $TAGS = ("" + $TAGS).toLowerCase();

        if ($TAGS.indexOf('@') > -1) {
            return false;
        }

        let $Permission = await Kuo_apijson.Permission();

        if (!$Permission[$TAGS]) {
            return true;
        }

        let Quanxian = $Permission[$TAGS][$method];

        if (!Quanxian || count(Quanxian) < 1) {
            return true;
        }

        if (Quanxian.indexOf('' + $Grade) == -1 && Quanxian.indexOf($Grade) == -1) {
            return true;
        }

        return false;

    }, Permission: async (off = 2) => {

        let Hash = 'apijson/Permission';
        if (off == 2) {
            if (count(Kuo_apijson.Permissiondata) > 0) {
                return Kuo_apijson.Permissiondata;
            }

            let HashData = await Mem.Get(Hash);

            if (HashData) {
                Kuo_apijson.Permissiondata = HashData;
                return HashData;
            }
        }

        let DB = await db("apijson_permission");
        let DBdata = await DB.PaiChu('id,atime,off,detail').Where({ off: 2 }).Select();
        if (!DBdata) {
            Kuo_apijson.Permissiondata = {};
            return {};
        }

        let HashData = {};
        for (var m in DBdata) {
            let data = DBdata[m];
            data.get = json_decode(data.get);
            data.put = json_decode(data.put);
            data.add = json_decode(data.add);
            data.del = json_decode(data.del);
            data.data = json_decode(data.data);
            data.prohibit = json_decode(data.prohibit);
            HashData[data.tags.toLowerCase()] = data;
        }

        Kuo_apijson.Permissiondata = HashData;
        await Mem.Set(Hash, HashData, Kuo_apijson.CacheTIme);
        return HashData;

    }, Rrequest: async (off = 2) => {
        let Hash = 'apijson/Rrequest';
        if (off == 2) {
            if (count(Kuo_apijson.Rrequestdata) > 0) {
                return Kuo_apijson.Rrequestdata;
            }

            let HashData = await Mem.Get(Hash);

            if (HashData) {
                Kuo_apijson.Rrequestdata = HashData;
                return HashData;
            }
        }

        let DB = await db("apijson_request");
        let DBdata = await DB.PaiChu('id,atime,off,detail').Where({ off: 2 }).Select();

        if (!DBdata) {
            Kuo_apijson.Rrequestdata = {};
            return {};
        }

        let HashData = { "get": {}, "put": {}, "add": {}, "del": {}, "data": {} };
        for (let dbd in DBdata) {
            let dbddata = DBdata[dbd];
            dbddata.method = json_decode(dbddata.method);
            dbddata.structure = json_decode(dbddata.structure);
            if (dbddata.method
                && count(dbddata.method) > 0
                && count(dbddata.structure) > 0
            ) {

                let czstructure = [];
                for (var ii in dbddata.structure) {
                    let iidata = dbddata.structure[ii];
                    if (iidata) {
                        if (iidata.z == "") {
                            iidata.z = iidata.n;
                        }

                        czstructure.push(iidata);
                    }
                }
                dbddata.tags = ('' + dbddata.tags).trim().toLowerCase();

                for (let meto in dbddata.method) {
                    let metodata = dbddata.method[meto];
                    if (!HashData[metodata][dbddata.tags]) {
                        HashData[metodata][dbddata.tags] = [];
                    }

                    HashData[metodata][dbddata.tags] = HashData[metodata][dbddata.tags].concat(czstructure);
                }



            }


        }
        Kuo_apijson.Rrequestdata = HashData;
        await Mem.Set(Hash, HashData, Kuo_apijson.CacheTIme);
        return HashData;

    }, RrequestGet: async ($TAGS = "get", $method = 'get') => {
        //获取表单处理
        $TAGS = ("" + $TAGS).toLowerCase();
        let Rrequest = await Kuo_apijson.Rrequest();
        if (!Rrequest[$method]) {
            return [];
        }

        if (!Rrequest[$method][$TAGS]) {
            return [];
        }

        return Rrequest[$method][$TAGS];


    }, AnalysisWhere: async (TYPE = 'find', $TAGS, $method, $Grade, Adata, $DATA, $_SESSION, $MM,$DATA_) => {
        /**
         * $TAGS 表单
         * $method put 数据
         * $Grade 角色id
         * Adata 复合式
         * $DATA 上次数据
         */
        let $TAGS_ = $TAGS;
        $TAGS = ("" + $TAGS).toLowerCase();
        let ZUHE = {
            page: 1,
            num: 10,
            order: [],
            zhicha: [],
            where: {},
            total: "/[]/total",  //统计返回
            table: $TAGS
        };
        let QITA  ={};
        if (TYPE == 'find') {
            ZUHE.num = 1;
        }
        
        ZUHE.table = await Kuo_apijson.PermissionGetTable($TAGS_);
        if (ZUHE.table == '') {
            if (TYPE == 'find') {
                return {};
            } else {
                return [];
            }
        }

        //获取处理数据
        let huodem = await Kuo_apijson.RrequestGet($TAGS, $method);
        //表单数据
        let buhan = await Kuo_apijson.PermissionGet($TAGS, $Grade);

        let qiangwhere = {};
        let buhan_ = [];
        let ZHUANHUAN = {};
        if (huodem && count(huodem) > 0) {
            for (var mm in huodem) {
                let mmd = huodem[mm];
                if (mmd.n != mmd.z) {
                    if (!ZHUANHUAN[mmd.n]) {
                        ZHUANHUAN[mmd.n] = ('' + mmd.z).toLowerCase();
                    }
                }
            }
        }

        if (buhan) {
            for (let bhk in buhan) {
                let bhkd = buhan[bhk];
                if (bhkd.indexOf('=') > -1) {
                    let mxx = bhkd.split('=');
                    if (mxx) {
                        let mxxk = ('' + mxx[0]).trim();
                        let mxxv = ('' + mxx[1]).trim();
                        if (mxxv.indexOf('#') > -1) {
                            let mxxv_ = Trim(mxxv, '#');
                            qiangwhere[mxxk] = mxxv_ in $_SESSION ? $_SESSION[mxxv_] : '';

                        } else {
                            qiangwhere[mxxk] = mxxv;
                        }
                    }

                } else {
                    buhan_.push(('' + bhkd).trim().toLowerCase());
                }
            }
        }

        if (count(qiangwhere) > 0) {
            let tmpqiangwhere = { "(": "and" };
            tmpqiangwhere = Object.assign({}, tmpqiangwhere, qiangwhere);
            tmpqiangwhere[')'] = "";
            qiangwhere = tmpqiangwhere;
        }

        let dbo = await db(ZUHE.table);
        let JieGou = await dbo.TableTab(ZUHE.table);
        if (JieGou) {
            ZUHE.zhicha = Object.keys(JieGou);
        }

        for (let k in Adata) {
            let kadata = Adata[k];
            if (k == '@column') {
                //只查
                let linshi = [];
                let zhichatmp = kadata.split(',');
                for (let zk_ in zhichatmp) {
                    let zk_da = ('' + zhichatmp[zk_]).trim().toLowerCase();
                    if (ZHUANHUAN[zk_da]) {
                        zk_da = ZHUANHUAN[zk_da];
                    }
                    if (ZUHE.zhicha.indexOf(zk_da) > -1) {
                        linshi.push(zk_da);
                    }
                }

                if (linshi && count(linshi) > 0) {
                    ZUHE.zhicha = linshi;
                }


            } else if (k == '@!column') {
                //排除
                let buhantmp = kadata.split(',');
                for (let zk_ in buhantmp) {
                    let zk_da = ('' + buhantmp[zk_]).trim().toLowerCase();
                    if (ZHUANHUAN[zk_da]) {
                        zk_da = ZHUANHUAN[zk_da];
                    }
                    buhan_.push(zk_da);
                }

            } else if (k == '@total') {

                ZUHE.total = kadata;

            } else if (k == '@page') {
                //页码
                ZUHE.page = Mode("Tools").Int(kadata);
                if (ZUHE.page < 1) {
                    ZUHE.page = 1;
                }

            } else if (k == '@count') {
                //条数
                ZUHE.num = Mode("Tools").Int(kadata);
                if (ZUHE.num < 1) {
                    ZUHE.num = 1;
                } else if (ZUHE.num > 100) {
                    ZUHE.num = 100;
                }
            } else if (k == '@order') {
                //排序
                let Adatak = kadata.replace(/ /gm, '');
                let Adatak_ = Adatak.split(',');
                for (var ad_ in Adatak_) {
                    let ad_da = Adatak_[ad_];
                    let huozi = "desc";

                    if (ad_da.indexOf('+') > -1) {
                        ad_da = ad_da.replace(/\+/gm, '');
                        huozi = "desc";
                    } else if (ad_da.indexOf('-') > -1) {
                        ad_da = ad_da.replace(/\-/gm, '');
                        huozi = "asc";
                    }

                    if (ZHUANHUAN[ad_da]) {
                        ad_da = ZHUANHUAN[ad_da];
                    }

                    if (JieGou[ad_da]) {
                        ZUHE.order[ad_da] = huozi;
                    }
                }

            }else if (k == '@where') {

                for(let kk_ in kadata){
                    let ZHIS = kadata[kk_];
                    if(ZHIS.indexOf("@") > -1){
                        let mx_ = Trim(ZHIS,'@');
                        ZHIS = $DATA[mx_];
                    }

                    if(kk_.indexOf(" ")>-1){
                        let qikai = kk_.split(" ");

                        if (ZHUANHUAN[qikai[0]]) {

                            qiangwhere[ kk_.replace(qikai[0],ZHUANHUAN[qikai[0]])   ] = ZHIS;

                        } else {
                            qiangwhere[kk_] = ZHIS;
                        }
                        
                    }else{

                        if (ZHUANHUAN[kk_]) {
                            qiangwhere[ZHUANHUAN[kk_]] = ZHIS;
                        } else {
                            qiangwhere[kk_] = ZHIS;
                        }
                    }
                   
                }

            } else {

                if(typeof(kadata) == "object"){
                    QITA[k] = kadata;
                }else{

                    if (ZHUANHUAN[k]) {
                        qiangwhere[ZHUANHUAN[k]] = kadata;
                    } else {
                        qiangwhere[k] = kadata;
                    }
                }
            }
        }


        ZUHE.where = qiangwhere;
        //不包含
        if (buhan_ && count(buhan_) > 0) {
            for (let x in buhan_) {
                let chid = ZUHE.zhicha.indexOf(buhan_[x]);
                if (chid > -1) {
                    ZUHE.zhicha.splice(chid, 1);
                }
            }
        }

        //隐藏表字段返回
        let NewZHUANHUAN = {};
        if (ZHUANHUAN && count(ZHUANHUAN) > 0) {
            for (let key in ZHUANHUAN) {
                if (ZHUANHUAN.hasOwnProperty(key)) {
                    NewZHUANHUAN[ZHUANHUAN[key]] = key;
                }
            }
        }

        let data = [];

        if (TYPE == 'find') {
            data = await dbo.Where(ZUHE.where).ZhiCha(ZUHE.zhicha).Limit(ZUHE.num, ZUHE.page).Order(ZUHE.order).Find();
            if (data) {

                if(QITA){
                   
                   for(var kkk in QITA){
                    let tyoss = 'find';
                    if(kkk.indexOf('[]')>-1){
                        tyoss = 'select';
                    }

                    data[kkk] = await  Kuo_apijson.AnalysisWhere (tyoss, kkk.replace('[]',''), $method, $Grade, QITA[kkk],data , $_SESSION, kkk,$DATA_);
                   }
                }


                for (let k in NewZHUANHUAN) {
                    let nk = NewZHUANHUAN[k];
                    data[nk] = data[k];
                    delete (data[k]);
                }

                for (let cfunc in huodem) {
                    let hmms = huodem[cfunc];
                    if (typeof (data[hmms.n]) != "undefined" && hmms.c != '') {
                        data[hmms.n] = await Kuo_apijson.GoShell(data[hmms.n], hmms.c, data,$_SESSION);
                    }
                }


            } else {
                data = {};
            }

        } else {

            data = await dbo.Where(ZUHE.where).ZhiCha(ZUHE.zhicha).Limit(ZUHE.num, ZUHE.page).Order(ZUHE.order).Select();
            let zongshu = 0;
            if (data) {
                zongshu = await dbo.Where(ZUHE.where).Total();
                for (let kk in data) {
                    let data_ = data[kk];

                    if(QITA){
                   
                        for(var kkk in QITA){
                            let tyoss = 'find';
                            if(kkk.indexOf('[]')>-1){
                                tyoss = 'select';
                            }
                            data_[kkk] = await  Kuo_apijson.AnalysisWhere (tyoss, kkk.replace('[]',''), $method, $Grade, QITA[kkk],data_ , $_SESSION, kkk,$DATA_);
                        }
                     }




                    for (let k in NewZHUANHUAN) {
                        let nk = NewZHUANHUAN[k];
                        data_[nk] = data_[k];
                        delete (data_[k]);
                    }

                    for (let cfunc in huodem) {
                        let hmms = huodem[cfunc];
                        if (typeof (data_[hmms.n]) != "undefined" && hmms.c != '') {
                            data_[hmms.n] = await Kuo_apijson.GoShell(data_[hmms.n], hmms.c, data_,$_SESSION);
                        }
                    }


                    data[kk] = data_;

                }

            } else {
                data = [];
            }
            if (!$DATA['total']) {
                $DATA['total'] = {};
                $DATA['page'] = {};
                $DATA['num'] = {};
            }
            $DATA['total'][$MM] = zongshu;
            $DATA['page'][$MM] = ZUHE.page;
            $DATA['num'][$MM] = ZUHE.num;

        }

        if (Debug) {
            console.log(ZUHE);
        }
        
        return data;

    }, Analysis: async (Arguments, $DATA, $method, $Grade, $_SESSION) => {

        //查询解析 $DATA 返回数据覆盖数据
        for (var m in Arguments) {
            let Adata = Arguments[m];
            let types = 'select';
            if (m.indexOf('[]') > -1) {

                if (!$DATA[m]) $DATA[m] = [];
                $DATA[m] =  await Kuo_apijson.AnalysisWhere(types, m.replace('[]', ''), $method, $Grade, Adata, $DATA, $_SESSION, m,$DATA);


            } else {

                $DATA[m] = {};
                types = 'find';
                if (Adata['@page']) {
                    types = 'select';
                }

                $DATA[m] = await Kuo_apijson.AnalysisWhere(types, m, $method, $Grade, Adata, $DATA, $_SESSION, m,$DATA);


            }
        }

      
    }, PermissionGetTable: async ($TAGS = "get") => {
        //获取真实表名字
        $TAGS = ("" + $TAGS).toLowerCase();
        let Permission = await Kuo_apijson.Permission();
        if (!Permission[$TAGS]) {
            return "";
        }
        return Permission[$TAGS]['name'] == "" ? $TAGS : Permission[$TAGS]['name'];

    }, PermissionGet: async ($TAGS = "get", $Grade = -1) => {
        //获取禁用字段或者固定Where
        $TAGS = ("" + $TAGS).toLowerCase();
        let Permission = await Kuo_apijson.Permission();
        if (!Permission[$TAGS]) {
            return [];
        }

        if (!Permission[$TAGS].prohibit) {
            return [];
        }

        let fanhui = [];
        if (Permission[$TAGS].prohibit['All']) {
            fanhui = Permission[$TAGS].prohibit['All'];
        }

        if (Permission[$TAGS].prohibit[$Grade]) {
            fanhui = fanhui.concat(Permission[$TAGS].prohibit[$Grade]);
        }

        if (count(fanhui) > 1) {
            fanhui = [...new Set(fanhui)];
        }

        return fanhui;
    }, GoShell: async ($data, $func, Data,$_SESSION) => {
        if (!$func || $func == "") {
            return $data;
        }

        $func = ('' + $func.replace(/函数:/gm, '')).trim();
        let zhisi = $func.split(':');

        let $hanshus = '$func_' + zhisi[0];

        if (!Kuo_apijson[$hanshus]) {
            return $data;
        }
        if (count(zhisi) == 1) {
            $data = await Kuo_apijson[$hanshus]($data);
        } else {
            zhisi.shift();
            for (let mx in zhisi) {
                let tzhis = zhisi[mx];
                if (tzhis == 'val') {
                    zhisi[mx] = $data;

                } else if (tzhis.indexOf('#') > -1) {

                    let keys = Trim(tzhis, '#');
                    if ($_SESSION[keys]) {
                        zhisi[mx] = $_SESSION[keys];
                    }

                } else if (tzhis.indexOf('@') > -1) {
                    let keys = Trim(tzhis, '@');
                    if (Data[keys]) {
                        zhisi[mx] = Data[keys];
                    }

                }
            }

            $data = await Kuo_apijson[$hanshus].apply(Data, zhisi);
        }

        return $data;

    }, $Where:async ($TAGS,Arguments,$DATA,$method,$Grade,$_SESSION) =>{
        let $DATA_ = Object.assign({},Arguments,$DATA);
        let biao = await Kuo_apijson.PermissionGetTable($TAGS);
        let biaodan =  await Kuo_apijson.RrequestGet($TAGS,"get");
        let caidan = await Kuo_apijson.PermissionGet($TAGS,$Grade);
        //排除表单数据
        let PaiChu = {};
        let WHERE = {};
        let ZIHAN = false;
        if( count(caidan) > 0){
            for(let m in caidan){
                let m_ = caidan[m];

                if((''+m_).indexOf('=') >-1){
                    let mxx = m_.split('=');
                    if (mxx) {
                        let mxxk = ('' + mxx[0]).trim();
                        let mxxv = ('' + mxx[1]).trim();
                        if (mxxv.indexOf('@') > -1) {

                            let mxxv_ = Trim(mxxv, '@');
                            if( typeof($DATA_[mxxv_]) != 'undefined' ){
                                WHERE[mxxk] = $DATA_[mxxv_];
                                ZIHAN = true;
                            }

                        }else if (mxxv.indexOf('#') > -1) {
                            let mxxv_ = Trim(mxxv, '#');
                            if( typeof($_SESSION[mxxv_]) != 'undefined'  ){
                                WHERE[mxxk] = $_SESSION[mxxv_];
                                ZIHAN = true;
                            }

                        }else{
                            WHERE[mxxk] = mxxv;
                            ZIHAN = true;
                        }
                    
                    }


                }else{
                    PaiChu[m_] = m_;
                   
                }
            }

        }
        if(ZIHAN) WHERE["("]="and";

        if(count(biaodan) > 0){
            for(let mx in biaodan){
                let mx_ = biaodan[mx];
                if(typeof(Arguments[mx_['n']]) != "undefined"){
                    

                    Arguments[mx_['z']] =
                    await Kuo_apijson.GoShell(Arguments[mx_['n']], mx_.c, $DATA_ ,$_SESSION   )

                    if(mx_['n'] != mx_['z']){
                        delete(Arguments[mx_['n']]);
                    }
                }
            }

        }
       
        for(var mx in Arguments){
            if(typeof(PaiChu[mx]) == "undefined"){

                let mm = Arguments[mx];
                if(mm.substring(0,1)  == '#'){
                    let mm_ = mm.substring(1);
                    WHERE[mx] =$_SESSION[mm_] ?$_SESSION[mm_]:"";
                }else if(mm.substring(0,1)  == '@'){
                    let mm_ = mm.substring(1);
                    WHERE[mx] =$DATA_[mm_] ?$DATA_[mm_]:"";
                }else{
                    WHERE[mx] = mm;
                }
            }
        }



        if(ZIHAN) WHERE[")"]="";


        return WHERE;

    }, $ADDATA:async ($TAGS,Arguments,$DATA,$method,$Grade,$_SESSION) =>{
        let $DATA_ = Object.assign({},Arguments,$DATA);
       
        let biaodan =  await Kuo_apijson.RrequestGet($TAGS,"get");
        let caidan = await Kuo_apijson.PermissionGet($TAGS,$Grade);
        //排除表单数据
        let PaiChu = {};
        let WHERE = {};
        let ZIHAN = false;
        if( count(caidan) > 0){
            for(let m in caidan){
                let m_ = caidan[m];

                if((''+m_).indexOf('=') >-1){
                    let mxx = m_.split('=');
                    if (mxx) {
                        let mxxk = ('' + mxx[0]).trim();
                        let mxxv = ('' + mxx[1]).trim();
                        if (mxxv.indexOf('@') > -1) {

                            let mxxv_ = Trim(mxxv, '@');
                            if( typeof($DATA_[mxxv_]) != 'undefined' ){
                                WHERE[mxxk] = $DATA_[mxxv_];
                                ZIHAN = true;
                            }

                        }else if (mxxv.indexOf('#') > -1) {
                            let mxxv_ = Trim(mxxv, '#');
                            if( typeof($_SESSION[mxxv_]) != 'undefined'  ){
                                WHERE[mxxk] = $_SESSION[mxxv_];
                                ZIHAN = true;
                            }

                        }else{
                            WHERE[mxxk] = mxxv;
                            ZIHAN = true;
                        }
                    
                    }


                }else{
                    PaiChu[m_] = m_;
                   
                }
            }

        }
      
        if(count(biaodan) > 0){
            for(let mx in biaodan){
                let mx_ = biaodan[mx];
                if(typeof(Arguments[mx_['n']]) != "undefined"){
                    

                    Arguments[mx_['z']] =
                    await Kuo_apijson.GoShell(Arguments[mx_['n']], mx_.c, $DATA_ ,$_SESSION   )

                    if(mx_['n'] != mx_['z']){
                        delete(Arguments[mx_['n']]);
                    }
                }
            }

        }
       
        for(var mx in Arguments){
            if(typeof(PaiChu[mx]) == "undefined"){

                let mm = Arguments[mx];
                if(mm.substring(0,1)  == '#'){
                    let mm_ = mm.substring(1);
                    WHERE[mx] =$_SESSION[mm_] ?$_SESSION[mm_]:"";
                }else if(mm.substring(0,1)  == '@'){
                    let mm_ = mm.substring(1);
                    WHERE[mx] =$DATA_[mm_] ?$DATA_[mm_]:"";
                }else{
                    WHERE[mx] = mm;
                }
            }
        }

        return WHERE;

    }, $YZDATA   :async ($TAGS,Arguments,$DATA,$method,$Grade,$_SESSION) => {
        let biao = await Kuo_apijson.PermissionGetTable($TAGS);
        let biaodan =  await Kuo_apijson.RrequestGet($TAGS,$method);
       
        let ZHIS = {};
     
        for(let mm in Arguments){
            let zhis = Arguments[mm];
            if (typeof (zhis) == 'object') {
                let bbb = await Kuo_apijson.$YZDATA(mm,zhis,$DATA,$method,$Grade,$_SESSION);
                if(bbb){
                    return bbb;
                }
            }else{
                ZHIS[mm] = zhis;
            }
        }

        if(count(ZHIS)  > 0 ){
            for(let m in ZHIS){

                if((''+ZHIS[m]).indexOf('@find')>-1){
                    let chaikai = ZHIS[m].split(':');
                    let $eee = {};
                    for(let u=2;u< chaikai.length-1;u++){
                        let zhis = chaikai[u];
                        let mx = zhis.split('=');
                        $eee[mx[0]] = mx[1];
                    }
                    let zhichas = chaikai[chaikai.length-1];
                    
                    let whwere =  await Kuo_apijson.$Where(chaikai[1],$eee,$DATA,$method,$Grade,$_SESSION);
                    let DBO = await db(biao);
                    let  data = await  DBO.Zhicha( zhichas ).Where(whwere).Find();
                    if(data){
                        ZHIS[m] = data[zhichas];
                    }

                    

                }else if((''+ZHIS[m]).indexOf('@')>-1){

                    let kyss = ZHIS[m].replace('@','');
                    if($DATA[kyss]){
                        ZHIS[m] = $DATA[kyss];
                    }

                }else  if((''+ZHIS[m]).indexOf('#')>-1){

                    let kyss = ZHIS[m].replace('#','');
                    if($_SESSION[kyss]){
                        ZHIS[m] = $_SESSION[kyss];
                    }

                }
            }

            for(let mmxx in biaodan){
                let shuju = biaodan[mmxx];
                if(shuju){
                    //必填
                    let bdan = shuju['n'];
                    if(shuju['b'] == 1){
                        if(!ZHIS[bdan]){
                            return $TAGS+' '+  bdan+" 必填";
                           
                        }

                        let yanzheng = await  Kuo_apijson.GzheYZ(shuju['g'],ZHIS[bdan]);
                        if(yanzheng){
                            return $TAGS+' '+  bdan +' '+ yanzheng;
                           
                        }

                    }else{

                        if( ZHIS[bdan] && ZHIS[bdan] != ""){

                            let yanzheng = await  Kuo_apijson.GzheYZ(shuju['g'],ZHIS[bdan]);
                            if(yanzheng){
                                return $TAGS+' '+ bdan +' '+ yanzheng; 
                            }
                        }
                    }
                }
            }
        }
        return false;

    }, $ADD   :async ($TAGS,Arguments,$DATA,$method,$Grade,$_SESSION) => {

        let biao = await Kuo_apijson.PermissionGetTable($TAGS);
        let biaodan =  await Kuo_apijson.RrequestGet($TAGS,$method);
        let caidan = await Kuo_apijson.PermissionGet($TAGS,$Grade);

        let ZHIS = {};
        for(let mm in Arguments){
            let zhis = Arguments[mm];
            if (typeof (zhis) == 'object') {
                await Kuo_apijson.$ADD(mm,zhis,$DATA,$method,$Grade,$_SESSION);
            }else{
                ZHIS[mm] = zhis;
            }
        }
       
        if(count(ZHIS)  > 0 ){
            for(let m in ZHIS){

                if((''+ZHIS[m]).indexOf('@find')>-1){
                    let chaikai = ZHIS[m].split(':');
                    let $eee = {};
                    for(let u=2;u< chaikai.length-1;u++){
                        let zhis = chaikai[u];
                        let mx = zhis.split('=');
                        $eee[mx[0]] = mx[1];
                    }
                    let zhichas = chaikai[chaikai.length-1];
                    
                    let whwere =  await Kuo_apijson.$Where(chaikai[1],$eee,$DATA,$method,$Grade,$_SESSION);
                    let DBO = await db(biao);
                    let  data = await  DBO.Zhicha( zhichas ).Where(whwere).Find();
                    if(data){
                        ZHIS[m] = data[zhichas];
                    }

                    

                }else if((''+ZHIS[m]).indexOf('@')>-1){

                    let kyss = ZHIS[m].replace('@','');
                    if($DATA[kyss]){
                        ZHIS[m] = $DATA[kyss];
                    }

                }else  if((''+ZHIS[m]).indexOf('#')>-1){

                    let kyss = ZHIS[m].replace('#','');
                    if($_SESSION[kyss]){
                        ZHIS[m] = $_SESSION[kyss];
                    }

                }
            }


         

            //禁用  =   biaodan,biao
            let CHARU = {};
            let PAICHU = {};
            for(let m in caidan){
                let m_ = caidan[m];
                if(m_){
                    if(m_.indexOf('=') >-1){
                        let mxx = m_.split('=');
                        if (mxx) {
                            let mxxk = ('' + mxx[0]).trim();
                            let mxxv = ('' + mxx[1]).trim();
                            if (mxxv.indexOf('#') > -1) {
                                let mxxv_ = Trim(mxxv, '#');
                                CHARU[mxxk] = mxxv_ in $_SESSION ? $_SESSION[mxxv_] : '';
                            } else {
                                CHARU[mxxk] = mxxv;
                            }
                        }

                    }else{
                        PAICHU[m_] = m_;
                    }
                }
            }

            for(let mmxx in biaodan){
                let shuju = biaodan[mmxx];
                if(shuju){
                    //必填
                    let bdan = shuju['n'];
                    if(shuju['b'] == 1){
                        if(!ZHIS[bdan]){
                            $DATA[$TAGS+'_'] = bdan+" 必填";
                            return ;
                        }

                        let yanzheng = await  Kuo_apijson.GzheYZ(shuju['g'],ZHIS[bdan]);
                        if(yanzheng){
                            $DATA[$TAGS+'_'] = bdan +' '+ yanzheng;
                            return ;
                        }

                    }else{

                        if( ZHIS[bdan] && ZHIS[bdan] != ""){
                            
                            let yanzheng = await  Kuo_apijson.GzheYZ(shuju['g'],ZHIS[bdan]);
                            if(yanzheng){
                                $DATA[$TAGS+'_'] = bdan +' '+ yanzheng;
                                return ;
                            }
                        }

                    }

                   
                    if(ZHIS[bdan]){
                        //{ n: 'a', z: 'a', b: '0', g: '', c: '' }
                        ZHIS[bdan] = await Kuo_apijson.GoShell(ZHIS[shuju.n], shuju.c, Object.assign({},$DATA,ZHIS ),$_SESSION   );
                        ZHIS[shuju['z']] = ZHIS[bdan]; 
                        if(bdan != shuju['z']){
                            delete(ZHIS[bdan]);
                        }
                    }
                    
                    
                }
            }

            for(let xx in PAICHU){
                delete(ZHIS[xx] );
            }

            for(let m in ZHIS){
                CHARU[m] = ZHIS[m];
            }
           
           

            let DBO = await db(biao);
            let JieGou = await DBO.TableTab(biao);

            for(let m in CHARU){
                if( typeof( JieGou[m]) == "undefined"){
                    delete(CHARU[m]);
                }
            }

            if(Debug){
                console.log($TAGS," Insert ",CHARU);
            }
            $DATA[$TAGS]  = await DBO.Insert(CHARU);

        }

    },GzheYZ :async (ge,zhis ) =>{
        if(!ge || ge == ''){
            return false;
        }
        if(!zhis){
            zhis = "";
        }

        if(ge =='mail'){
            //邮箱
            let phoneRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
            if (! phoneRegex.test(zhis)) {
                return "邮箱错误";
            }

        }else if(ge =='shouji'){
            //手机
            let phoneRegex = /^1[3-9]\d{9}$/;
            if (! phoneRegex.test(zhis)) {
                return "手机号错误";
            }

        }else if(ge =='shfen'){
            //身份证
            let phoneRegex = /^\d{17}[\dXx]$/;
            if (! phoneRegex.test(zhis)) {
                return "身份证错误";
            }

        }else if(ge.indexOf('函数')>-1){

            ge = ('' + ge.replace(/函数:/gm, '')).trim();
            let $hanshus = '$func_' +ge;
            if( Kuo_apijson[$hanshus]){

                let fanhui = await Kuo_apijson[$hanshus](zhis);

                if(fanhui){

                    return "值错误";
                }

            }else{
                return '函数 '+ ge+" 不存在";
            }
            


        }else if(ge.indexOf('正则')>-1){
            
            ge = ('' + ge.replace(/正则:/gm, '')).trim();
            let phoneRegex =new RegExp(ge);
        
            if (! phoneRegex.test(zhis)) {
                return "身份证错误";
            }

        }else if(ge.indexOf('-')>-1){
            let mm = ge.split('-');
            let xiao = parseInt(mm[0]);
            let da = parseInt(mm[1]);
            if(zhis.length < xiao || zhis.length >da){
                return "格式错误[ "+ge+" ]";
            }

        }else if(ge != ''){

            ge = parseInt(ge);
            if(zhis.length != ge){
                return "格式错误["+ge+"]";
            }
        }

        return false;
    }
    /**自定义函数**/
    , $func_pichttp: pichttp  //图片加上cdn
    , $func_pichttplv: pichttplv //过滤掉cdn前缀
    , $func_time: TIME  //生成时间
    , $func_md5: Md5  //生成md5
    , $func_strtotime: strtotime  //日期转时间戳
    , $func_mima: Kuomm  //生成变异密码
    , $func_hmima: async (...canshu) => { //生成变异md5
        return Kuomm(canshu.join('_'));
    },$func_hash: async (...canshu) => {
        return Md5(canshu.join('_'));
    }
    , $func_date: Mode("Tools").Date  //生成Y-m-d时间
    , $func_explode: explode //split ,
    , $func_jsonen: json_encode //json 压缩
    , $func_jsonde: json_decode //json 美化
    , outhine() {

    }

}